
package com.lihao.demo1.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;


public class BaseCompat {

    private Context context;

    public BaseCompat(Context context) {
        this.context = context;
    }




    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }




    public void startActivityClearTask(Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }

    public void startActivityNewTask(Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public void startActivityClearTask(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public void startActivity(Class<?> cls, Bundle extras) {
        Intent intent = new Intent(context, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtras(extras);
        context.startActivity(intent);
    }

    public void startActivityForResult(Class<?> cls, int requestCode) {
        Intent intent = new Intent(context, cls);
            ((Activity)context).startActivityForResult(intent, requestCode);
    }

    public void startActivityForResult(Class<?> cls, int requestCode, Bundle extras) {
        Intent intent = new Intent(context, cls);
        intent.putExtras(extras);
            ((Activity)context).startActivityForResult(intent, requestCode);
    }






    public void setDrawablePosition(TextView textView, int left, int top, int right, int bottom, int bounds) {
        Drawable drawableTop = null;
        Drawable drawableLeft = null;
        Drawable drawableRight = null;
        Drawable drawableBottom = null;
        int padding = 10;
        if (top != 0) {
            drawableTop = getDrawable(textView, top, padding, bounds);
        }
        if (left != 0) {
            drawableLeft = getDrawable(textView, left, padding, bounds);
        }
        if (right != 0) {
            drawableRight = getDrawable(textView, right, padding, bounds);
        }
        if (bottom != 0) {
            drawableBottom = getDrawable(textView, bottom, padding, bounds);
        }
        textView.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
    }

    public void setDrawableLeft(TextView textView, int resId, int padding, int bounds) {
        Drawable drawable = getDrawable(textView, resId, padding, bounds);
        textView.setCompoundDrawables(drawable, null, null, null);
    }

    public void setDrawable(TextView textView, Drawable left, Drawable right, Drawable top, Drawable bottom){
        textView.setCompoundDrawables(left, top, right, bottom);
    }

    public void setDrawableLeft(TextView textView, int resId, int padding, int width, int height) {
        Drawable drawable = getDrawable(textView, resId, padding, width, height);
        textView.setCompoundDrawables(drawable, null, null, null);
    }

    public Drawable getDrawable(TextView textView, int resId, int p, int b) {
        if (resId <= 0) {
            return null;
        }
        int padding = convertDp(p);
        if (padding > 0) {
            textView.setCompoundDrawablePadding(padding);
        }
        return getDrawable(resId,p,b);
    }

    public Drawable getDrawable(int resId, int p, int b) {

        if (resId <= 0) {
            return null;
        }

        int padding = convertDp(p);
        int bounds = convertDp(b);

        Drawable drawable = ContextCompat.getDrawable(context, resId);

        if (drawable != null) {
            if (bounds <= 0) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
                int height = bitmap.getHeight();
                int width = bitmap.getWidth();
                drawable.setBounds(0, 0, width, height);
            } else {
                drawable.setBounds(0, 0, bounds, bounds);
            }
        }

        return drawable;
    }

    private Drawable getDrawable(TextView textView, int resId, int p, int w, int h) {

        if (resId <= 0) {
            return null;
        }
        int padding = convertDp(p);
        int wbounds = convertDp(w);
        int hbounds = convertDp(h);

        Drawable drawable = ContextCompat.getDrawable(context, resId);

        if (drawable != null) {
            if (hbounds <= 0 || wbounds <= 0) {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
                int height = bitmap.getHeight();
                int width = bitmap.getWidth();
                drawable.setBounds(0, 0, width, height);
            } else {
                drawable.setBounds(0, 0, wbounds, hbounds);
            }
        }
        if (padding > 0) {
            textView.setCompoundDrawablePadding(padding);
        }
        return drawable;
    }

    public void setDrawableRight(TextView textView, int resId, int padding, int bounds) {
        Drawable drawable = getDrawable(textView, resId, padding, bounds);
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    public void setDrawableRight(TextView textView, int resId, int padding, int width, int height) {
        Drawable drawable = getDrawable(textView, resId, padding, width, height);
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    public void setDrawableTop(TextView textView, int resId, int padding, int bounds) {
        Drawable drawable = getDrawable(textView, resId, padding, bounds);
        textView.setCompoundDrawables(null, drawable, null, null);
    }

    public void setDrawableBottom(TextView textView, int resId, int padding, int bounds) {
        Drawable drawable = getDrawable(textView, resId, padding, bounds);
        textView.setCompoundDrawables(null, null, null, drawable);
    }

    public void setDrawableBottom(TextView textView, int resId, int padding, int width, int height) {
        Drawable drawable = getDrawable(textView, resId, padding, width,height);
        textView.setCompoundDrawables(null, null, null, drawable);
    }

    public int convertDp(int px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        int result = (int) ((px / 3.0) * scale);
        return result;
    }



}
